using SharpDX.DirectInput;
using System;

namespace Engine.Input
{
    /// <summary>
    /// The Keyboard device is handled here
    /// </summary>
    public class KeyboardInput
    {
        #region Private Fields

        private bool forwardPressed, rewindPressed, leftPressed, rightPressed, turnrightPressed, turnleftPressed, camTurnRight, camTurnLeft, space;
        private Keyboard keyboard = null;

        #endregion Private Fields

        #region Public Constructors

        /// <summary>
        /// Creates a new Keyboard-Device.
        /// </summary>
        /// <param name="form">the form where the Device is assign to</param>
        public KeyboardInput(IntPtr handlePointer)
        {
            //Form formm = new Form();
            //formm.Show();
            DirectInput di = new DirectInput();
            keyboard = new Keyboard(di);
            keyboard.SetCooperativeLevel(handlePointer, CooperativeLevel.NonExclusive | CooperativeLevel.Background);
            keyboard.Acquire();
        }

        #endregion Public Constructors

        #region Public Properties

        /// <summary>
        /// Checks if CamTurnLeft-Key is pressed
        /// </summary>
        public bool CamTurnLeft
        {
            get
            {
                return camTurnLeft;
            }
        }

        /// <summary>
        /// Checks if CamTurnRight-Key is pressed
        /// </summary>
        public bool CamTurnRight
        {
            get
            {
                return camTurnRight;
            }
        }

        /// <summary>
        /// Checks if forward-key is pressed
        /// </summary>
        public bool forword
        {
            get
            {
                return forwardPressed;
            }
        }

        /// <summary>
        /// Checks if left-Key is pressed
        /// </summary>
        public bool left
        {
            get
            {
                return leftPressed;
            }
        }

        /// <summary>
        /// Checks if backward-Key is pressed
        /// </summary>
        public bool rewind
        {
            get
            {
                return rewindPressed;
            }
        }

        /// <summary>
        /// Checks if rights-Key is pressed
        /// </summary>
        public bool right
        {
            get
            {
                return rightPressed;
            }
        }

        /// <summary>
        /// Checks if Spacebar-Key is pressed
        /// </summary>
        public bool Spacebar
        {
            get
            {
                return space;
            }
        }

        /// <summary>
        /// Checks if turnleft-Key is pressed
        /// </summary>
        public bool turnleft
        {
            get
            {
                return turnleftPressed;
            }
        }

        /// <summary>
        /// Checks if turnright-Key is pressed
        /// </summary>
        public bool turnright
        {
            get
            {
                return turnrightPressed;
            }
        }

        #endregion Public Properties

        #region Public Methods

        /// <summary>
        /// read keys from the Keyboard-device
        /// </summary>
        public void Update()
        {
            if (keyboard.IsDisposed)
                return;
            KeyboardState state = keyboard.GetCurrentState();

            forwardPressed = state.IsPressed(Key.W);
            rewindPressed = state.IsPressed(Key.S);
            leftPressed = state.IsPressed(Key.A);
            rightPressed = state.IsPressed(Key.D);
            turnleftPressed = state.IsPressed(Key.Q);
            turnrightPressed = state.IsPressed(Key.E);
            camTurnRight = state.IsPressed(Key.X);
            camTurnLeft = state.IsPressed(Key.Y);
            space = state.IsPressed(Key.Space);
        }

        #endregion Public Methods

        #region Internal Methods

        internal void Dispos()
        {
            this.keyboard.Dispose();
        }

        #endregion Internal Methods
    }
}